﻿using Admin.Infra;
using Admin.Models;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Admin.Controllers
{
    public class AssetController : Controller
    {
        public readonly IncludesDBContext includeContext = new IncludesDBContext();

        public ActionResult Upload()
        {
            var viewModel = new ViewModel();
            viewModel.Includes = includeContext.Includes.FindAll().ToList();
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            var include = new Include();
            include._id = ObjectId.GenerateNewId().ToString();
            include.CreatedDate = DateTime.Now;
            include.ModifiedDate = DateTime.Now;
            include.SortOrder = 0;
            include.FileName = file.FileName;
            include.Type = Path.GetExtension(file.FileName);
            //includeContext.Includes.CreateIndex(IndexKeys<Include>.Ascending(p => p.FileName));
            try
            {
                includeContext.Includes.Save(include, WriteConcern.Acknowledged);
                file.SaveAs(@"C:\CatNav\Google Code\temp\" + file.FileName);
            }
            catch (MongoException ex)
            {
                return RedirectToAction("upload");
            }
            return RedirectToAction("index");
        }


    }
}
